﻿#ifndef __NTRIP_DEAL_H__
#define __NTRIP_DEAL_H__

#include "stdaxf.h"
#include "GPSDeal.h"
#include <thread>
#include <vector>
#include "common/serialportdatasource.h"
#include "NtripCallback.h"

#ifdef SWIG
#else
#ifdef WIN32
typedef void(__stdcall *NtripMsgCallback)(int code, const std::string &msg);
#else
typedef void(NTRIP_EXPORT *NtripMsgCallback)(int code, const std::string &msg);
#endif // WIN32

#endif // SWIG
#define NtripBufferCount 8192

const std::string NTRIP_DEFAULT_NODE = "Download Source Table";
enum ConnectType
{
	Tcp=0,
	Ntrip,
	Udp
};
enum NtripCode
{
	NtrpUnknown = -1,
	NtpError,
	NtpInfo,
	NtpWarrnning,
	NtpDiff,
	NtpMount,
	NtpDebug
};
enum DataSourceNtrip
{
	Software=0,
	Serialport
};
class NTRIP_EXPORT NtripDeal
{
public:
	NtripDeal();
	NtripDeal(const std::string &path);
	NtripDeal(const std::string &path, int type, int source);
    ~NtripDeal();
private:
	void initConnect(const std::string &path, int type, int source);
	void initDefalut();
public:
	// 需要外部定义定时器处理该函数，不同的系统定时器机制有差异
	void timeTick();
	void connectNtrip(const std::string &ipcaster,int ipport,const std::string &node,const std::string &name,const std::string &pwd);
	void connectTcp(const std::string &ipcaster, int ipport);
	void disconnectNtrip();
	void init();
	void setCallBack(NtripBaseCallback *call) { this->mCallBack = call; }
	void ReceiveNMEA(int type,const std::string &nmea, bool isManual);
	void SaveSetting();
	static std::string MostRecentGGA;
	bool hasSourceTable();
private:
	const std::vector<char> Nothing;
	const std::string vbCrLf = "\r\n";
	const std::string vbCr = "\r";
	const std::string vbLf = "\n";
    void NtripCallback(int code, const std::string &msg, NtripMsgCallback call);
    void DiffCallBack(int code, const std::vector<char> &msg, int count, NtripMsgCallback call);
    void LoadSettingsFile();
	void LogEvent(const std::string &msg);
	void startNtrip();
	void StopNTRIP();
	
	void NTRIPLoop();
	void NTRIPUpdateUIThread( int Item,const std::string &Value, const std::vector<char> &mBytes);
	std::string generateRequestMessage();
	void ParseSourceTable(const std::string &table);
	void SendSerialLineToUIThread(const std::string &nmea);
	bool IsNumeric(std::string str);
	void GPSLoop();

private:
	NtripBaseCallback *mCallBack;
public:
	int NTRIPProtocol;
	std::string NTRIPCaster;
	int NTRIPPort;
	std::string NTRIPUsername;
	std::string NTRIPPassword;
	std::string NTRIPMountPoint;
#ifdef SWIG
#else
    NtripMsgCallback callBack;
#endif // SWIG
private:
	GPSDeal *mGpsDeal;
	std::thread *NTRIPThread;
	std::thread *GPSThread;
	SerialPortDataSourceTool *mSerialPort;
	bool NTRIPIsConnected;
	int StartNTRIPThreadIn;
	int NTRIPConnectionAttempt;
	bool NTRIPShouldBeConnected;
	bool NTRIPStreamRequiresGGA;
	bool NTRIPUseManualGGA;
	double NTRIPManualLat;
	double NTRIPManualLon;
	int NTRIPByteCount;
	std::string mSettingsfile;
	std::string PreferredMountPoint;
	//std::string NTRIPStreamArray[1][2];
	std::vector<std::vector<std::string> > NTRIPStreamArray;
	DataSourceNtrip mDataSource=Software;
	ConnectType mConnectType=Ntrip;
};

#endif // !__NTRIP_DEAL_H__
